/**
 * @(#)YNoteOAuth2Client.java, 2013-5-24. 
 * 
 * Copyright 2013 Yodao, Inc. All rights reserved.
 * YODAO PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package outfox.ynote.oauth2.client;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import net.oauth.OAuth;
import net.oauth.OAuthConsumer;
import net.sf.json.JSONObject;

import org.apache.http.HttpResponse;

import outfox.ynote.open.client.YNoteClient;
import outfox.ynote.open.client.YNoteException;
import outfox.ynote.open.client.YNoteHttpUtils;

/**
 *
 * @author limeng
 *
 */
public class YNoteOAuth2Client extends YNoteClient {

    /**
     * @param consumer
     */
    public YNoteOAuth2Client(OAuthConsumer consumer) {
        super(consumer);
    }

    @Override
    public String grantRequestToken(String callbackURL) throws IOException,
            YNoteException {
        throw new RuntimeException("not implemented");
    }

    @Override
    public void grantAccessToken(String code) throws IOException,
            YNoteException {
        throw new RuntimeException("not implemented");
    }

    @Override
    public void setAccessToken(String accessToken, String tokenSecret) {
        throw new RuntimeException("not implemented");
    }
    
    public String getAuthorizationURL(String redirectURL, String state)
            throws IOException, YNoteException {
        String url = OAuth.addParameters(
                accessor.consumer.serviceProvider.userAuthorizationURL,
                "client_id", accessor.consumer.consumerKey);
        url = OAuth.addParameters(url, "response_type", "code");
        url = OAuth.addParameters(url, "redirect_uri", redirectURL);
        url = OAuth.addParameters(url, "state", state);
        return url;
    }
    
    public String grantAccessToken(String code, String redirectURL)
            throws IOException, YNoteException {
        Map<String, String> parameters = new HashMap<String, String>();
        parameters.put("code", code);
        parameters.put("redirect_uri", redirectURL);
        parameters.put("grant_type", "authorization_code");
        parameters.put("client_id", accessor.consumer.consumerKey);
        parameters.put("client_secret", accessor.consumer.consumerSecret);
        
        HttpResponse response = YNoteHttpUtils.doGet(
                accessor.consumer.serviceProvider.accessTokenURL,
                null, parameters);
        // extract the access token
        String content = YNoteHttpUtils.getResponseContent(
                response.getEntity().getContent());
        return JSONObject.fromObject(content).getString("accessToken");
    }
    
    public void setAccessToken(String accessToken) {
        this.accessor.accessToken = accessToken;
    }

    @Override
    protected String getBaseURL() {
        return super.getBaseURL();
    }

    @Override
    protected HttpResponse doGet(String url, Map<String, String> parameters)
            throws IOException, YNoteException {
        Map<String, String> oauth2Parameters = new HashMap<String, String>();
        oauth2Parameters.put(OAuth.OAUTH_TOKEN, accessor.accessToken);
        return YNoteHttpUtils.doGet(url, parameters, oauth2Parameters);
    }

    @Override
    protected HttpResponse doPostByMultipart(String url,
            Map<String, Object> parameters) throws IOException, YNoteException {
        Map<String, String> oauth2Parameters = new HashMap<String, String>();
        oauth2Parameters.put(OAuth.OAUTH_TOKEN, accessor.accessToken);
        return YNoteHttpUtils.doPostByMultipart(url, parameters, oauth2Parameters);
    }

    @Override
    protected HttpResponse doPostByUrlEncoded(String url,
            Map<String, String> parameters) throws IOException, YNoteException {
        Map<String, String> oauth2Parameters = new HashMap<String, String>();
        oauth2Parameters.put(OAuth.OAUTH_TOKEN, accessor.accessToken);
        return YNoteHttpUtils.doPostByUrlEncoded(url, parameters, oauth2Parameters);
    }
}
